package com.insurance.productcase.service.impl;

import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.common.enums.StateEnum;
import com.insurance.productcase.dao.ProductCaseDao;
import com.insurance.productcase.entity.ProductCaseEntity;
import com.insurance.productcase.service.ProductCaseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;



@Service("productCaseService")
public class ProductCaseServiceImpl extends ServiceImpl<ProductCaseDao, ProductCaseEntity> implements ProductCaseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductCaseEntity> page = this.page(
                new Query<ProductCaseEntity>().getPage(params),
                new QueryWrapper<ProductCaseEntity>()
        );

        return new PageUtils(page);
    }

    public QueryWrapper<ProductCaseEntity> getQueryWrapper(){
        QueryWrapper<ProductCaseEntity> queryWrapper = new QueryWrapper<ProductCaseEntity>();
        queryWrapper.eq("delete_flag", String.valueOf(DeleteFlagEnum.UN_DELETE.getCode()));
        queryWrapper.eq("state", StateEnum.UP.getCode());
        queryWrapper.orderByAsc("order_number");
        return queryWrapper;
    }

    public QueryWrapper<ProductCaseEntity> getQueryWrapper(ProductCaseEntity productCaseEntity){
        QueryWrapper<ProductCaseEntity> queryWrapper = getQueryWrapper();
        if(!StringUtils.isBlank(productCaseEntity.getProductId())){
            queryWrapper.eq("product_id", productCaseEntity.getProductId());
        }
        return queryWrapper;
    }

    /**
     * 通过产品主键查询方案
     *
     * @param productId 产品主键
     * @return
     */
    @Override
    public List<ProductCaseEntity> queryListByProductId(String productId){
        ProductCaseEntity productCaseEntity = new ProductCaseEntity();
        productCaseEntity.setProductId(productId);
        QueryWrapper<ProductCaseEntity> queryWrapper = getQueryWrapper(productCaseEntity);
        return this.list(queryWrapper);
    }

}